package com.eling.elcms.cater.webapp.controller;

import com.eling.elcms.cater.model.Dish;
import com.eling.elcms.cater.model.Dish.State;
import com.eling.elcms.cater.service.IDishService;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DishController extends BaseFormController{
	@Autowired
	private IDishService dishService;
	
	@RequestMapping("/api/cater/dish/search*")
	@ResponseBody
	public List<Dish> sreach(@ModelAttribute SearchCondition sc,@ModelAttribute Dish dish) {
		sc.setQueryCondition(dish);
		return dishService.search(sc); 
	}
	
	@RequestMapping("/api/cater/dish/query")
    @ResponseBody
    public List<Dish> query(@ModelAttribute Dish cond){
    	return dishService.query(cond); 
    }
	
	@RequestMapping("/api/cater/dish/save")
    @ResponseBody
    public Dish save(@ModelAttribute@ModelFilling Dish dish) {
		if(dish.getPkDish() == null) {
			dish.setState(State.Sale);
		}
		return dishService.save(dish);
    }
	
	@RequestMapping("/api/cater/dish/{pkDish}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkDish) {
		dishService.remove(pkDish);
	    return new Msg("删除成功");
	}
	
	@RequestMapping("/api/cater/dish/{pkDish}/sale")
	@ResponseBody
	public Msg sale(@PathVariable Long pkDish) {
		if(dishService.sale(pkDish)) {
			return new Msg("true");
		} else {
			return new Msg("false");
		}
	    
	}
	
	/**
	 * <p>查询是否有套餐包含该菜品</p>
	 * <br>
	 * @param pkDish
	 * @return
	 * @author ZhangPeng
	 * 2016年12月26日
	 */
	@RequestMapping("/api/cater/dish/{pkDish}/querysetmealdish")
	@ResponseBody
	public Msg querySetMealDish(@PathVariable Long pkDish) {
		//TODO 该逻辑现在未完成。
		if(dishService.querySetMealDish(pkDish)) {
			return new Msg("true");
		} else {
			return new Msg("false");
		}
	}

	@RequestMapping("/api/cater/dish/{pkDish}/stopsale")
	@ResponseBody
	public Msg stopSale(@PathVariable Long pkDish) {
		//TODO 停售逻辑中套餐联带停售没做。
		if(dishService.stopSale(pkDish)) {
			return new Msg("true");
		} else {
			return new Msg("false");
		}
	}

}
